package com.hxx.day8;

import org.junit.Test;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

/**
 * Stream流
 */
public class TestDemo3 {
    /**
     * 获取Stream流
     */
    @Test
    public void test1(){
        //1.集合获取Stream流
        List<String> list = List.of("a", "b", "c");
        Stream<String> stream1 = list.stream();
        stream1.forEach(System.out::println);
    }
    /**
     * 数组获取Stream流
     */
    @Test
    public void test2(){
        //2.数组获取Stream流
        String[] arr = {"a", "b", "c"};
        Arrays.stream(arr).forEach(System.out::println);
    }
    /**
     * 使用Stream的静态方法创建流
     */
    @Test
    public void test3(){
        //3.流的操作
        Stream<String> stream = Stream.of("a", "b", "c");
        stream.forEach(System.out::println);
    }
    /**
     * 无限流
     */
    @Test
    public void test4(){
        //4.无限流
        Stream<Integer> stream = Stream.iterate(5, n -> n + 2);
        stream.limit(10).forEach(System.out::println);
    }

    @Test
    public void test5(){
        List.of("a", "b", "c").stream().forEach(System.out::println);
        Map.of("a", 1, "b", 2, "c", 3).forEach((k, v) -> System.out.println(k + "=" + v));
    }
    /**
     * 流的操作
     */
    @Test
    public void test6(){
        //流的操作
        List.of(1, 2, 3, 4, 2, 6, 3, 8, 9, 10).stream()
                .filter(n -> n % 2 == 0)//筛选出偶数
                .distinct()//去重
                .skip(2)//跳过前2个元素
                .limit(3)//取前3个元素
                .forEach(System.out::println);
    }
}












































